package wifiunion.com.notice.widget.selectmemberdialog.presenter;


import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;

import wifiunion.com.notice.entity.SubNoticeTargetEntity;
import wifiunion.com.notice.widget.selectmemberdialog.SelectMemberContact;
import wifiunion.com.notice.widget.selectmemberdialog.entity.SelectMember;
import wifiunion.com.notice.widget.selectmemberdialog.model.SelectModel;

/**
 * Created by Administrator on 2018/7/9.
 */

public class SelectMemberPresenter<T extends SelectMemberContact.SelectMemberView> implements SelectMemberContact.Presenter<T>, SelectMemberContact.SelectMemberCallback {
    /**
     * 0 获取分组
     * 1 获取分组成员
     * 2 添加通知成员
     */
    private int type;

    /**
     * 父级uuid
     */
    private String parentuuid;
    /**
     * 请求页面类型
     * 0 通知
     */
    private int pagetype;

    private SelectModel selectModel;


    WeakReference<T> viewRef;

    public SelectMemberPresenter(int type, String parentuuid, int pagetype, T view) {
        this.type = type;
        this.parentuuid = parentuuid;
        this.pagetype = pagetype;
        this.selectModel = new SelectModel();
        viewRef = new WeakReference<T>(view);
    }

    @Override
    public void start() {

    }

    @Override
    public void start(HashMap<String, Object> params) {
        if (type == 0) {
            selectModel.getRelGroupList(params, this, pagetype);
        } else if (type == 1) {
            selectModel.getRelGoupMemberList(params, this, pagetype);
        } else if (type == 2) {
            selectModel.addMemberRequest(params, this, pagetype);
        }
    }


    @Override
    public void onSuccess(Object data) {
        ArrayList<SelectMember> result = null;
        if (type != 2) {

            if (pagetype != 3) {
                result = (ArrayList<SelectMember>) data;

                if (result != null && !result.isEmpty()) {
                    for (SelectMember sm : result) {
                        sm.parentUuid = parentuuid;
                    }
                }
            } else {
                //添加识别对象 获取通知对象的流程，因为数据结构跟其他的差异较大，须单独处理
                if (type == 1) {
                    try {
                        ArrayList<SubNoticeTargetEntity> originResult = (ArrayList<SubNoticeTargetEntity>) data;
                        result = new ArrayList<>();
                        for (SubNoticeTargetEntity sEntity : originResult) {
                            SelectMember sm = new SelectMember(0, sEntity.getUuid(), sEntity.getShowName1(), parentuuid, sEntity.getIsSelected(), sEntity.getShowName2(), sEntity.getPicUrl(), sEntity.getPicUrl());
                            result.add(sm);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                else{
                    result = (ArrayList<SelectMember>) data;

                    if (result != null && !result.isEmpty()) {
                        for (SelectMember sm : result) {
                            sm.parentUuid = parentuuid;
                        }
                    }
                }
            }
        }
        if (isViewEnable()) {
            if (type == 0) {
                viewRef.get().getGroupSuccess(result);
            } else if (type == 1) {
                viewRef.get().getMemberSuccess(result);
            } else if (type == 2) {
                viewRef.get().addMemberSuccess(data);
            }
        }
    }

    @Override
    public void onServerFailed(Object errorObj) {
        if (isViewEnable()) {
            if (type == 0) {
                viewRef.get().getGroupFailed(errorObj);
            } else if (type == 1) {
                viewRef.get().getMemberFailed(errorObj);
            } else if (type == 2) {
                viewRef.get().addMemberFailed(errorObj);
            }
        }
    }

    @Override
    public void onNetFailed() {

    }

    @Override
    public void onAttach(T t) {
        viewRef = new WeakReference<T>(t);
    }

    @Override
    public void onDetach() {
        if (viewRef != null && viewRef.get() != null) {
            viewRef.clear();
            viewRef = null;
        }
    }

    public boolean isViewEnable() {
        if (viewRef != null && viewRef.get() != null) {
            return true;
        }
        return false;
    }
}
